/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Susliawan
 */
public class Utility {

    public static boolean isFileExist(String filename) throws FileNotFoundException, IOException {
        boolean temp = false;

        FileInputStream fstream = new FileInputStream("file.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        
        while((strLine = br.readLine()) != null){
            String delims = "[#]";
            String[] tokens = strLine.split(delims);
            if(filename.equals(tokens[0]))
                temp = true;
        }

        return temp;
    }
    
     public static void saveTorrent(String name, String filelength, String piecelength, String npieces) throws IOException {
        FileWriter fstream = new FileWriter("file.txt", true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(name + "#" + filelength + "#" + piecelength + "#" + npieces);
        out.newLine();
        out.close();
    }
}
